
package controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import model.Gnome;

/**
 *This class represents a client's basket where a client can add or delete articles (gnomes)
 * @author Guillaume, Yann
 */
@ConversationScoped
@Named

public class Basket implements Serializable {

    @Inject
    private StoreHouse storeHouse;
    //K=type,V=nbr gnome
    private HashMap<String, Integer> wantedItems = new HashMap<String, Integer>();
    //K=type;V=price
    private HashMap<String, Double> priceByGnomeType = new HashMap<String, Double>();

    /**
     * This function can be used to add a certain amount of the same gnome type in the client basket
     * @param gnome the type of gnome
     * @param amount the quantity added in the basket
     */
    public void addItemsToTheBasket(Gnome gnome, int amount) {
        Integer nbrWantedGnome;
        if (wantedItems.get(gnome.getGnomeType()) != null) {
            nbrWantedGnome = amount + wantedItems.get(gnome.getGnomeType());
        } else {
            nbrWantedGnome = amount;
        }
        wantedItems.put(gnome.getGnomeType(), nbrWantedGnome);
        priceByGnomeType.put(gnome.getGnomeType(), gnome.getGnomePrice());
    }

    /**
     * This function can be used to add a certain amount of the same gnome type in the client basket
     * @param type the type of the gnome
     * @param amount quantity added
     * @param price price of this type of gnome
     */
    public void addItemsToTheBasket(String type, int amount, Double price) {
        Integer nbrWantedGnome;
        if (wantedItems.get(type) != null) {
            nbrWantedGnome = amount + wantedItems.get(type);
        } else {
            nbrWantedGnome = amount;
        }
        wantedItems.put(type, nbrWantedGnome);
        priceByGnomeType.put(type, price);
    }
    /**
     * This function can be used to delete a quantity of gnome from the client basket
     * @param gnome the type of gnome to delete
     * @param amount the amount to delete
     */
    public void deleteItemsToTheBasket(Gnome gnome, int amount) {
        Integer nbrWantedGnome = wantedItems.get(gnome.getGnomeType()) - amount;
        wantedItems.put(gnome.getGnomeType(), nbrWantedGnome);
    }
    /**
     * This function can be used to delete a quantity of gnome from the client basket
     * @param type
     * @param amount 
     */
    public void deleteItemsToTheBasket(String type, int amount) {
        Integer nbrWantedGnome = wantedItems.get(type) - amount;
        wantedItems.put(type, nbrWantedGnome);
    }
    /**
     * To obtain the hashMap which stores the wanted items
     * @return HashMap wanted items
     */
    public HashMap<String, Integer> getListOfGnomeType() {
        return wantedItems;
    }
    /**
     * To obtain the price for a certain type of gnome
     * @param type
     * @return the price for the type
     */
    public Double getPriceForGnomeTypeX(String type) {
        return priceByGnomeType.get(type);
    }
    /**
     * To obtain the total price for all gnome of a certain type
     * @param type
     * @return the price for all gnomes of this type
     */
    public Double valueForAllGnomeTypeX(String type) {
        Integer nbrGnomeTypeX = wantedItems.get(type);
        Double priceTypeX = priceByGnomeType.get(type);
        return nbrGnomeTypeX * priceTypeX;
    }
    /**
     * To obtain the price for all gnomes in the basket
     * @return the total price
     */
    public Double priceForAllGnome() {
        Set set = wantedItems.keySet();
        Double totalPrice = 0.0;
        for (Iterator it = set.iterator(); it.hasNext();) {
            String type = (String) it.next();
            totalPrice = totalPrice + valueForAllGnomeTypeX(type);
        }
        return totalPrice;
    }
    /**
     * This function should be called when someone wants to buy items in his basket
     */
    public void buy() {
        Set set = wantedItems.keySet();
        for (Iterator it = set.iterator(); it.hasNext();) {
            String type = (String) it.next();
            Integer amount = wantedItems.get(type);
            if (storeHouse.deleteAmountGnomeTypeX(type, amount)) {
                deleteItemsToTheBasket(type, amount);
                System.out.println("You bought" + type);
            } else {
                System.out.println("Not enough gnomes " + type + " in the storehouse");
            }
        }

    }
}
